﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using HA.ModFlow.Domain.FileLayer;

namespace HA.ModFlow.Domain.DomainLayer
{
    [Serializable]
   public class DrainCollection:BoundaryCollecionBase
   {


        public DrainCollection(int nrows, int ncolumns, int nlayers)
            : base(nrows, ncolumns, nlayers)
        {
            this._innerList = new List<Drain>();
 
        }



        List<Drain> _innerList;

        public List<Drain> InnerList
        {
            get { return _innerList; }
            set { _innerList = value; }
        }


        List<Drain> _tempDrains = new List<Drain>();
        bool _haveTempData = false;
        public bool HaveTempData
        {
            get { return _haveTempData; }
        }
        
        public void AddTempDrains(List<Drain> drains)
        {
            this._tempDrains.AddRange(drains);
            this._haveTempData = true;
        }


        /// <summary>
        /// 将暂存的数据提供给核心，并清空对应的选项
        /// </summary>
        public void CommitTempDrains()
        {
            if (this._haveTempData)
            {
                this.ResetDrains(this._tempDrains);
                this._tempDrains.Clear();
            }
            this._haveTempData = false;
        }


       #region 公共函数


        public List<Drain> GetDrain(int index)
        {
            List<Drain> temp = new List<Drain>();

            foreach (Drain item in this._innerList)
            {
                if (item.Index == index)
                {
                    temp.Add(item);

                }

            }

            return temp;


        }

        public void UpdateDrains(List<Drain> drains,List<int> indices)
        {
            List<Drain> orgHeads = new List<Drain>();

            foreach (int index in indices)
            {
                orgHeads.AddRange(this.GetDrain(index));

            }


            foreach (Drain head in orgHeads)
            {
                this._innerList.Remove(head);


            }


            this._innerList.AddRange(drains);


            this.AskCollectionChanged(new SCColleconChangedEventArgs(this._innerList, SCCollectionChangedType.RESET, SCType.Drain));


        }

        public void ResetDrains(List<Drain> drains)
        {
            this._innerList.Clear();

            this._innerList.AddRange(drains);

            this.AskCollectionChanged(new SCColleconChangedEventArgs(this._innerList, SCCollectionChangedType.RESET, SCType.Drain));


 
        }


       public PDrain ToDrainPack()
        {
            PDrain p = new PDrain();

            p.MXACTD = this._innerList.Count;
            //p.IDRNCB = 0;



            for (int i = 0; i < this.NPeriods; i++)
            {

                SPDrain sp = new SPDrain();



                if (i == 0)
                {

                    sp.ITMP = this._innerList.Count;

                    foreach (Drain item in this._innerList)
                    {
                        GPointValue pv = new GPointValue(item.K, item.I, item.J, item.Elevation[i], item.Conductance[i]);


                        sp.DrainData.Add(pv);

                    }


                }

                else
                {

                    if (this.IsUsePrecious[i] == true)
                    {
                        sp.ITMP = -1;

                    }
                    else
                    {

                        sp.ITMP = this._innerList.Count;

                        foreach (Drain item in this._innerList)
                        {
                            GPointValue pv = new GPointValue(item.K, item.I, item.J, item.Elevation[i], item.Conductance[i]);


                            sp.DrainData.Add(pv);

                        }

                    }
                }


                p.StressPeriodDrain.Add(sp);



            }
            


            return p;

        }
       public override void _stressPeriods_StressPeriodChanged(object sender, StressPeriodChangedEventArgs e)
       {

           //基类的函数必须调用

           base._stressPeriods_StressPeriodChanged(sender, e);






           List<StressPeriod> lst = (List<StressPeriod>)e.Param;           
           foreach (Drain item in this._innerList)
           {
               item.OnResetSperiods(lst.Count);
           }
       }


       public void Clear()
       {

           this._innerList.Clear();

           this.AskCollectionChanged(new SCColleconChangedEventArgs(this._innerList, SCCollectionChangedType.RESET, SCType.Drain));



       }

       public override void OnGridReset(int nrows, int ncols, int nlayers)
       {
           base.OnGridReset(nrows, ncols, nlayers);

           this.Clear();

       }
       #endregion

   }
}
